package com.tiny.core;

import org.apache.log4j.Logger;
import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.tiny.controller.DictionaryController;
import com.tiny.controller.LoginController;
import com.tiny.controller.MenuController;
import com.tiny.controller.PermissionController;
import com.tiny.controller.RoleController;
import com.tiny.controller.SysLogController;
import com.tiny.controller.UserController;
import com.tiny.handler.GlobalHandler;
import com.tiny.interceptor.ManagerInterceptor;
import com.tiny.model.Dictionary;
import com.tiny.model.Menu;
import com.tiny.model.MenuPermission;
import com.tiny.model.Permission;
import com.tiny.model.Role;
import com.tiny.model.RolePermission;
import com.tiny.model.SysLog;
import com.tiny.model.User;
import com.tiny.model.UserRole;
import com.tiny.shiro.ShiroExt;
import com.tiny.thread.ThreadSysLog;

public class TinyConfig extends JFinalConfig {

	private static Logger log = Logger.getLogger(TinyConfig.class);
	private Routes routes;
	private ShiroPlugin shiroPlugin;

	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用getProperty(...)获取值
		log.info("configConstant loadPropertyFile");
		loadPropertyFile("init.properties");
		me.setDevMode(getPropertyToBoolean("devMode", false));

		log.info("configConstant 视图Beetl设置");
		me.setMainRenderFactory(new BeetlRenderFactory());
		GroupTemplate gt = BeetlRenderFactory.groupTemplate;
		gt.registerFunctionPackage("so", new ShiroExt());

		log.info("configConstant 视图error page设置");
		me.setError401View("/common/401.html");
		me.setError403View("/common/403.html");
		me.setError404View("/common/404.html");
		me.setError500View("/common/500.html");

		me.setMaxPostSize(100 * 10485760);
	}

	public void configRoute(Routes me) {
		me.add("/", LoginController.class);
		me.add("/syslog", SysLogController.class);
		me.add("/user", UserController.class);
		me.add("/role", RoleController.class);
		me.add("/permission", PermissionController.class);
		me.add("/menu", MenuController.class);
		me.add("/dict", DictionaryController.class);

		this.routes=me;
	}

	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),
				getProperty("user"), getProperty("password").trim());
		me.add(c3p0Plugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);

		arp.addMapping("User", User.class);
		arp.addMapping("Role", Role.class);
		arp.addMapping("Permission", Permission.class);
		arp.addMapping("SysLog", SysLog.class);
		arp.addMapping("Menu", Menu.class);
		arp.addMapping("MenuPermission", MenuPermission.class);
		arp.addMapping("Dictionary", Dictionary.class);
		arp.addMapping("RolePermission", RolePermission.class);
		arp.addMapping("UserRole", UserRole.class);
		/**Map<String, ActiveRecordPlugin> arpMap = CFKit.newMap();
		arpMap.put("mysql", arp);
		new TablelPlugin(arpMap).start();**/

		// 配置shiro插件
		shiroPlugin = new ShiroPlugin(this.routes);
		me.add(shiroPlugin);	
	}

	public void configInterceptor(Interceptors me) {
		me.add(new ShiroInterceptor());
		me.add(new ManagerInterceptor());
	}

	public void configHandler(Handlers me) {
		log.info("configHandler 全局配置处理器，主要是记录日志和request域值处理");
		me.add(new GlobalHandler());
	}

	/**
	 * 系统启动完成后执行
	 */
	public void afterJFinalStart() {
		ThreadSysLog.startSaveDBThread();
		
		/**DefaultPasswordService ps=new DefaultPasswordService();
		Record user = new Record().set("username", "admin").set("password", ps.encryptPassword("admin")).set("status", 1); 
		Db.save("user", user); **/
	}

	/**
	 * 系统关闭前调用
	 */
	public void beforeJFinalStop() {
		ThreadSysLog.setThreadRun(false);
	}

	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目 运行此 main
	 * 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 10);
	}
}
